package org.springblade.modules.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springblade.modules.platform.entity.SysJobLog;
import org.springblade.modules.platform.mapper.SysJobLogMapper;
import org.springblade.modules.platform.service.ISysJobLogService;
import org.springblade.modules.platform.vo.SysJobLogListVO;
import org.springblade.core.mp.support.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 定时任务调度日志表 服务实现类
 * </p>
 *
 * @author SYG
 * @since 2022-08-24
 */
@Service
public class SysJobLogServiceImpl extends ServiceImpl<SysJobLogMapper, SysJobLog> implements ISysJobLogService {

	@Override
	public void addJobLog(SysJobLog jobLog) {
		jobLog.setCreateTime(new Date());
		baseMapper.insert(jobLog);

	}

	/**
	 * 批量删除调度日志信息
	 *
	 * @param logIds 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteJobLogByIds(Long[] logIds) {
		return baseMapper.deleteBatchIds(Lists.newArrayList(logIds));
	}

	@Override
	public int deleteJobLogById(Long jobId) {
		return baseMapper.deleteById(jobId);
	}

	@Override
	public void cleanJobLog() {

		baseMapper.cleanJobLog();
	}

	@Override
	public SysJobLogListVO selectJobLogList(SysJobLog jobLog, Query query) {
		SysJobLogListVO sysJobListVO = new SysJobLogListVO();
		LambdaQueryWrapper<SysJobLog> queryWrapper = new LambdaQueryWrapper();
		if(jobLog.getJobLogId()!=null){
			queryWrapper.eq(SysJobLog::getJobLogId,jobLog.getJobLogId());
		}
		if(!StringUtils.isEmpty(jobLog.getJobName())){
			queryWrapper.like(SysJobLog::getJobName,jobLog.getJobName());
		}
		Page<SysJobLog> sysJobPage = baseMapper.selectPage(new Page<SysJobLog>(query.getCurrent(), query.getSize()), queryWrapper);
		long total = sysJobPage.getTotal();
		sysJobListVO.setTotal(total);
		sysJobListVO.setRecords(sysJobPage.getRecords());

		return sysJobListVO;
	}


	/**
	 * 通过调度任务日志ID查询调度信息
	 *
	 * @param jobLogId 调度任务日志ID
	 * @return 调度任务日志对象信息
	 */
	@Override
	public SysJobLog selectJobLogById(Long jobLogId) {
		return baseMapper.selectById(jobLogId);
	}
}
